package com.ibiaoke.customer.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.customer.entity.Deposit;
import com.ibiaoke.customer.entity.DepositExample;
import com.ibiaoke.customer.mapper.DepositMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2017/6/19.
 */
@Repository
public class DepositDao extends BaseDao<Deposit,DepositExample,Integer,DepositMapper>{

    @Autowired
    private DepositMapper depositMapper;

    @Override
    public DepositMapper getMapper() {
        return depositMapper;
    }

    public List<Deposit> listDeposit(Integer customerId, Page page) {
        DepositExample example = new DepositExample();
        DepositExample.Criteria criteria = example.createCriteria();
        if (customerId != null) {
            criteria.andCustomerIdEqualTo(customerId);
        }

        page.setTotalCount(depositMapper.countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());

        List<Deposit> depositList = depositMapper.selectByExample(example);
        return depositList;
    }

    public Deposit getDeposit(Integer customerId) {
        DepositExample example = new DepositExample();
        example.createCriteria().andCustomerIdEqualTo(customerId);
        return getByExample(example);
    }

    public int depositPay(BigDecimal price, Integer customerId) {
        DepositExample example = new DepositExample();
        example.createCriteria().andCustomerIdEqualTo(customerId);
        Deposit deposit = new Deposit();
        deposit.setPreDeposit(price);
        return depositMapper.updateByExampleSelective(deposit, example);
    }

    public int updatePreDeposit(Integer depositId, BigDecimal preDeposit) {
        Deposit deposit = new Deposit();
        deposit.setDepositId(depositId);
        deposit.setPreDeposit(preDeposit);
        return updateByPrimaryKeySelective(deposit);
    }

    public List<Deposit> depositList(List<Integer> customerIds) {
        DepositExample example = new DepositExample();
        example.createCriteria().andCustomerIdIn(customerIds);
        return selectByExample(example);
    }
}
